# 作者: YYJ
# 2025年07月27日14时48分35秒
# 2486249622@qq.com
import logging

def log_console():
    # logging.*** 后面会显示出来的内容优先级>= logging.***
    logging.basicConfig(level=logging.WARNING,
                        format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    # 开始使用 log 功能
    # 不会显示
    logging.debug('这是 loggging debug message')
    logging.info('这是 loggging info message')
    # 会显示
    logging.warning('这是 loggging a warning message')
    logging.error('这是 an loggging error message')
    logging.critical('这是 loggging critical message')

def log_file():
    logging.basicConfig(level=logging.INFO,
        filename='test.txt',
        filemode='w',
        encoding='utf-8',
        format='%(asctime)s - %(filename)s[line:%(lineno)d] -%(levelname)s: %(message)s')
    # use logging
    logging.debug('这是 loggging debug message')
    logging.info('这是 loggging info message')

    logging.warning('这是 loggging a warning message')
    logging.error('这是 an loggging error message')
    logging.critical('这是 loggging critical message')

def log_all():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)   # log总开关

    logfile = 'log.txt'
    fh = logging.FileHandler(logfile, mode='a', encoding='utf-8')
    fh.setLevel(logging.DEBUG)  # 文件日志开关

    ch = logging.StreamHandler()
    ch.setLevel(logging.WARNING)    # 控制台日志开关

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(ch)

    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')


if __name__ == '__main__':
    # log_console()
    # log_file()
    log_all()
